import DWC_DCContentItem, { DWC_DCContentData } from "./DWC_DCContentItem/DWC_DCContentItem";

const { ccclass, property } = cc._decorator;
/** 复杂聊天 聊天item list */
@ccclass
export default class DWC_DCCMsgList extends cc.Component {
    /** scrollView 的 item -> DWC_DCContentItem */
    @property(cc.Prefab)
    itemTemplate: cc.Prefab = null;
    /** scrollView */
    @property(cc.ScrollView)
    scrollView: cc.ScrollView = null;
    /** 表情图集 */
    @property(cc.SpriteAtlas)
    atlas_emoticon: cc.SpriteAtlas = null;
    /** 最大item个数 */
    @property(cc.Integer)
    spawnCount = 7;
    /** item 间隔 */
    @property(cc.Float)
    spacing = 10;
    /** 缓冲区高度，超过这个才会重新设置 */
    @property(cc.Float)
    bufferZone = 400;
    /** item 最初的高 */
    @property(cc.Float)
    initItemH = 110;
    /** scrollView 滑动内容区 */
    private content: cc.Node = null;
    /** 上一次content位置 */
    private lastContentPosY = null;
    /** scrollView需要显示的数据 */
    private _data: DWC_DCContentData[] = null;
    /** scrollView 是否正在滚动 */
    private isMoveing: boolean = null;
    onLoad() {
        this.lastContentPosY = 0;
        this.content = this.scrollView.content;
    }
    /** 初始化数据 */
    initData(_list: DWC_DCContentData[]): void {
        if (!_list) return;
        this.showHideItem();
        this._data = null;
        this._data = _list;
        this.content.height = 0;
        this.content.height = this.getHeightListInContent(this._data);
        this.setList();
    }
    /** 添加item */
    addItem(_itemDatas: DWC_DCContentData[], _isPush: boolean = true): void {
        _isPush && this._data.push(..._itemDatas);
        this.content.height += this.getHeightListInContent(_itemDatas) - this.spacing;
        this.setList();
    }
    /** 显示或隐藏item */
    showHideItem(_isShow: boolean = false): void {
        this.content && this.content.children.forEach(_v => _v.active = _isShow);
    }
    /** 设置列表 */
    setList(): void {
        let _idx = this._data.length >= this.spawnCount ? this._data.length - this.spawnCount : 0;
        if (_idx >= 0) {
            let _childrenIdx: number = 0;
            for (let _i = _idx; _i < this._data.length; _i++) {
                //设置item node
                let _node: cc.Node = null;
                if (this.content.children[_childrenIdx]) {
                    _node = this.content.children[_childrenIdx];
                } else {
                    _node = cc.instantiate(this.itemTemplate);
                    _node.setParent(this.content);
                }
                _childrenIdx++;
                _node.active = true;
                //设置item 数据
                let _item: DWC_DCContentItem = _node.getComponent(_node.name);
                this.setItemData(_item, this._data[_i]);
                //设置位置
                if (this._data[_i].itemY > 0) {
                    _node.setPosition(0, -this.getY(_i));
                    _item.dccontent && (_item.dccontent.itemY = _node.y);
                    this._data[_i].itemY = _node.y;
                } else {
                    _node.setPosition(0, this._data[_i].itemY);
                }
                if (this._data[_i].height < 0) {
                    this._data[_i].height = _node.height;
                }
                _item.itemIdx = _i;
            }
            this.trunToBottom();
        } else {
            return null;
        }
    }
    /** 获得item y 位置 */
    getY(_idx: number = 0): number {
        let _pitY: number = 0;
        if (_idx >= 0) {
            //---生成一个item
            let _node = cc.instantiate(this.itemTemplate);
            _node.setParent(this.content);
            let _item: DWC_DCContentItem = _node.getComponent(_node.name);
            //设置高度
            for (let _i = 0; _i < this._data.length; _i++) {
                if (_i <= _idx) {
                    if (this._data[_i].height < 0) {
                        this.setItemData(_item, this._data[_i]);
                        _pitY += _i == _idx ? this.initItemH / 2 : _item.node.height + this.spacing;
                    } else {
                        _pitY += _i == _idx ? this.initItemH / 2 : this._data[_i].height + this.spacing;
                    }
                } else {
                    break;
                }
            }
            this.content.removeChild(_node);
            _pitY += this.spacing;
        }
        return _pitY;
    }
    /** 跳转到最下面 */
    trunToBottom(): void {
        let _h = this.content.height, _viewh = this.scrollView.node.height;
        this.content.setPosition(0, _h - _viewh > 0 ? _h - _viewh / 2 : _viewh / 2);
    }
    /** 滚动组件是否滚动 */
    scrollMove(_isMoveing: boolean = false): void {
        this.isMoveing = _isMoveing;
    }
    update(_dt) {
        if (!this.isMoveing) return;
        //---设置显示
        let _bfr = this.bufferZone;
        let _isDown = this.scrollView.content.y < this.lastContentPosY;
        let _viewh = this.scrollView.node.height;
        //设置显示
        this.content.children.forEach(_v => {
            let _viewp: cc.Vec2 = this.getPitInView(_v);//获取每个item的坐标
            //向下滑动
            if (_isDown) {
                if (_viewp.y < -_bfr - _viewh / 2) {
                    let _item: DWC_DCContentItem = _v.getComponent(_v.name);
                    //设置item数据
                    let _curIdx = _item.itemIdx - this.content.childrenCount;
                    if (_curIdx >= 0 && _curIdx < this._data.length) {
                        this.setItemData(_item, this._data[_curIdx]);
                        _v.setPosition(0, -this.getY(_curIdx));
                        _item.itemIdx = _curIdx;
                    }
                }
            } else {//向上滑动
                if (_viewp.y > _bfr + _viewh / 2) {
                    //设置item数据
                    let _item: DWC_DCContentItem = _v.getComponent(_v.name);
                    let _curIdx = _item.itemIdx + this.content.childrenCount;
                    if (_curIdx < this._data.length) {
                        this.setItemData(_item, this._data[_curIdx]);
                        _v.setPosition(0, -this.getY(_curIdx));
                        _item.itemIdx = _curIdx;
                    }
                }
            }
        });
        this.lastContentPosY = this.scrollView.content.y;
    }
    /** 获得item相对于view的位置 */
    getPitInView(_node: cc.Node): cc.Vec2 {
        let _wp: cc.Vec2 = _node.parent.convertToWorldSpaceAR(_node.position);
        return this.scrollView.node.convertToNodeSpaceAR(_wp);
    }
    /** 获得某数据放到内容区,内容区的高(把数据放进去的实际高) */
    getHeightListInContent(_list: DWC_DCContentData[]): number {
        let _contentH: number = 0;
        if (!_list || _list.length <= 0) return _contentH;
        //---生成一个item
        let _node = cc.instantiate(this.itemTemplate);
        _node.setParent(this.content);
        let _item: DWC_DCContentItem = _node.getComponent(_node.name);
        //----设置高度
        _list.forEach(_v => {
            if (_v.height < 0) {
                this.setItemData(_item, _v);
                _contentH += _item.node.height + this.spacing;
            } else {
                _contentH += _v.height + this.spacing;
            }
        });
        this.content.removeChild(_node);
        return _contentH - this.spacing;
    }
    /** 设置玩家数据 */
    setItemData(_item: DWC_DCContentItem, _data: DWC_DCContentData): void {
        _item.initui();
        _item.init(_data);
        //设置聊天内容类型
        switch (_data.type) {
            case 1://文本
                _item.setChatLblContent(_data.content);
                break;
            case 2://表情
                _item.setEmoticonContent(this.atlas_emoticon.getSpriteFrame(_data.content));
                break;
            case 3://语音
                break;
        }
        _item.dccontent && (_item.dccontent.height = _item.node.height);
    }
}
